package org.j4me.deviceinfo;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.CharBuffer;
import java.util.Enumeration;
import java.util.Vector;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.j4me.deviceinfo.data.Device;
import org.j4me.deviceinfo.data.DeviceDb;
/**
 * Servlet implementation class for Servlet: CollectorServlet
 *
 * @web.servlet name="CollectorServlet" display-name="Name for
 *              portal" description="Description for portal"
 * @web.servlet-mapping url-pattern="/collect"
*/

public class CollectorServlet extends HttpServlet {
	
	static Logger _logger = Logger.getLogger(CollectorServlet.class);
	private DeviceDb _db;
	
	
	@Override
	public void init(ServletConfig config) throws ServletException {
		_db = DeviceDb.factory("/home/tomcat/j4me/devices.xml");
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		_logger.info("---------------------------------------------------");
		 Vector infos = new Vector();
		  int len =req.getContentLength();
		  
		  
		  
		  Enumeration names = req.getHeaderNames();
		  while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			_logger.info("HTTP header " + name +"=" + req.getHeader(name));
			
		}
		  
		  
		  infos.add(new String [] { "user-agent" , req.getHeader("User-agent")});
		  
			
		   DataInputStream in = new DataInputStream(req.getInputStream());
		   int count = in.readInt();
		   for(int i = 0; i < count ; i++)
		   {
			   String key = in.readUTF();
			   String value = in.readUTF();
			   _logger.info(key + "=" +  value);
			   infos.add(new String [] { key , value});
		   }
		   
//		  BufferedReader reader = new BufferedReader(new InputStreamReader(req.getInputStream()));
//		  char[] cbuf;
//		  if(len > 0)
//		  {
//			  cbuf  = new char[len];
//			  
//			  int readed = 0;
//			  while(readed < len)
//			  {
//				  int pass = reader.read(cbuf,readed,len-readed);
//				  if( pass == -1)
//					  break;
//				  else
//				
//					  readed += pass;
//			  }
//		  }
//		  else
//		  {
//			  StringBuffer buf = new StringBuffer();
//			cbuf = new char[20000];
//			len = 0;
//			int c;
//			while ((c = reader.read()) >= 0 && len < cbuf.length) {
//				cbuf[len] = (char) c;
//				len++;
//			}
//			buf.append(cbuf);
//			_logger.info("get "+ len + " ["+ buf.toString() +"]");
//					  
//		  }
//		  reader.close();
//		  
//		  int pos = 0;
//		  while(pos < len)
//		  {
//			  short size = (short) cbuf[pos];
//			  StringBuffer buffer = new StringBuffer();
//			  buffer.append(cbuf, pos+1, size);
//			  String key = buffer.toString();
//			  pos += 1+size;
//			  size = (short) cbuf[pos];
//			  buffer = new StringBuffer();
//			  buffer.append(cbuf, pos+1, size);
//			  String value = buffer.toString();
//				_logger.info(key + "=" +  value);
//			  infos.add(new String [] { key , value});
//			  pos += 1+size;
//		  }
//		  
		  
		  
		  
		  _db.addDevice(new Device(infos));
		  _db.save();
		  
		 
		  
		  
		
	}
		
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		doGet(req, resp);
	}
}
